package com.xrf;

import com.sys.common.util.Query;
import com.sys.common.util.ResultMsg;
import com.sys.sys.entity.ArticleEntity;
import com.sys.sys.service.ArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工资核算内容信息表
 */
@RestController
@RequestMapping("/sys/article")
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:article:list")
    public ResultMsg list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ArticleEntity> articleList = articleService.queryList(query);
        int total = articleService.queryTotal(query);
        
        ResultMsg resultMsg = ResultMsg.ok();
        resultMsg.put("total", total);
        resultMsg.put("rows", articleList);
        return resultMsg;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{articleId}")
    @RequiresPermissions("sys:article:info")
    public ResultMsg info(@PathVariable("articleId") Integer articleId) {
        ArticleEntity article = articleService.queryObject(articleId);
        return ResultMsg.ok().put("article", article);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:article:save")
    public ResultMsg save(@RequestBody ArticleEntity article) {
        if (article == null || article.getArticleType() == null || 
            article.getContent() == null || article.getAuthor() == null) {
            return ResultMsg.error("请输入完整的参数");
        }
        if (article.getCreateTime() == null) {
            article.setCreateTime(new Date());
        }
        articleService.save(article);
        return ResultMsg.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:article:update")
    public ResultMsg update(@RequestBody ArticleEntity article) {
        articleService.update(article);
        return ResultMsg.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:article:delete")
    public ResultMsg delete(@RequestBody Integer[] articleIds) {
        articleService.deleteBatch(articleIds);
        return ResultMsg.ok();
    }
}